package com.piper.core;

import com.piper.core.exception.AbstractServiceException;

import java.io.Serializable;

/**
 * 返回给前台的通用包装
 */
public class ResultData implements Serializable {

    public static final Integer SUCCESS_CODE = 1;

    public static final Integer ERROR_CODE = -1;

    public static final String SUCCESS_MESSAGE = "操作成功";

    public static final String ERROR_MESSAGE = "网络异常";

    /**
     * 请求是否成功
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应对象
     */
    private Object data;

    public ResultData() {
    }

    public ResultData(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static ResultData success() {
        return new ResultData(SUCCESS_CODE, SUCCESS_MESSAGE, null);
    }

    public static ResultData success(Object object) {
        return new ResultData(SUCCESS_CODE, SUCCESS_MESSAGE, object);
    }

    public static ResultData success(String message, Object object) {
        return new ResultData(SUCCESS_CODE, message, object);
    }

    public static ResultData error() {
        return new ResultData(ERROR_CODE, ERROR_MESSAGE, null);
    }

    public static ResultData error(String message) {
        return new ResultData(ERROR_CODE, message, null);
    }

    public static ResultData error(Object object) {
        return new ResultData(ERROR_CODE, ERROR_MESSAGE, object);
    }

    public static ResultData error(String message, Object object) {
        return new ResultData(ERROR_CODE, message, object);
    }

    public static ResultData error(AbstractServiceException serviceException) {
        return new ResultData(serviceException.getCode(), serviceException.getMessage(), null);
    }

}
